package com.ptszyxx.popose.module.test.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.module.test.OnItemLoaded;
import com.ptszyxx.popose.module.test.entity.MarqueeItem;
import com.ysg.utils.YImageUtil;

import java.util.ArrayList;
import java.util.List;

public class MarqueeRecyclerViewAdapter extends RecyclerView.Adapter<MarqueeRecyclerViewAdapter.ViewHolder> {
    private final OnItemLoaded onItemLoaded;
    private final Context context;
    private List<MarqueeItem> items;

    public MarqueeRecyclerViewAdapter(Context context, OnItemLoaded onItemLoaded) {
        items = new ArrayList<>();
        this.context = context;
        this.onItemLoaded = onItemLoaded;
    }

    public void setItems(List<MarqueeItem> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    public void load() {
        onItemLoaded.onItemLoaded();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.marquee_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MarqueeItem item = items.get(position);

        YImageUtil.show(holder.icon, item.getIconUrl());
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        final ImageView icon;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
        }
    }
}
